// ---------------------------------------------------------------------
//
// -- 技能展示
// -- author £
//
// ---------------------------------------------------------------------

const BREAK_DELAY = 0.1,
    BLAST_DELAY = 0;

cc.Class({
    extends: cc.Component,

    properties: {
        hammer: cc.Node,
        hammerRegion: cc.Node,
        hammerAn: cc.Animation,
        gridNode: cc.Node,
        bombAn: cc.Animation,
        bomb: cc.Node,
        bombRegion: cc.Node,
        bombP: cc.ParticleSystem,
        skillHammerArea: cc.Node,
        skillBombArea: cc.Node,
        lasers: {
            default: [],
            type: require('LaserSkill'),
        }
    },

    ctor: function () {
        this._hammerX = 0;
        this._hammerY = 0;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.gridSpine = this.gridNode.getComponent('sp.Skeleton');
        this.hammerRegion.active = false;
        this.hammerAn.node.active = false;
        this.hammer.active = false;
        this.bombAn.node.active = false;
        this.bomb.active = false;
        this.gridNode.active = false;
        this._hammerX = this.hammer.x = this.skillHammerArea.x;
        this._hammerY = this.hammer.y = this.skillHammerArea.y;
        this._bombX = this.bomb.x = this.skillBombArea.x;
        this._bombY = this.bomb.y = this.skillBombArea.y;
    },

    start () {

    },

    showLaserSkill: function (data) {
        let item, i, len = this.lasers.length;

        for (i = 0; i < len; ++i) {
            item = data[i];
            this.lasers[i].show(item.x, item.y, item.d);
        }
    },

    reset: function () {
        let i, len = this.lasers.length;

        for (i = 0; i < len; ++i) {
            this.lasers[i].hide();
        }

        this.hideHammer();
        this.hammerAn.node.stopAllActions();
        this.hammerAn.stop();
        this.hammerAn.node.active = false;
        this.gridNode.active = false;
        this.hideBomb();
        this.bombAn.node.stopAllActions();
        this.bombAn.stop();
        this.bombAn.node.active = false;
        this.bombP.stopSystem();
    },

    showHammer: function (hammerOffset) {
        this.hammer.active = true;
        this.hammerRegion.active = false;
        this.hammer.x = this._hammerX;
        this.hammer.y = this._hammerY + hammerOffset;
    },

    hideHammer: function () {
        this.hammer.active = false;
        this.hammerRegion.active = false;
    },

    updateHammerPos: function (x, y) {
        this.hammer.x = this._hammerX + x;
        this.hammer.y = this._hammerY + y;
    },

    updateHammerRegion: function (x, y) {
        if (x === null) {
            this.hammerRegion.active = false;
            return;
        }

        this.hammerRegion.x = x;
        this.hammerRegion.y = y;
        this.hammerRegion.active = true;
    },

    showHammerSkill: function (x, y, colorIndex) {
        this.hideHammer();
        this.hammerAn.node.active = true;
        this.gridNode.active = true;
        this.hammerAn.node.stopAllActions();
        this.hammerAn.play();
        this.hammerAn.node.x = x;
        this.hammerAn.node.y = y;
        this.gridNode.x = x;
        this.gridNode.y = y;
        require('CommonUtil').delay(
            this.hammerAn.node,
            BREAK_DELAY,
            function () {
                this.gridSpine.setAnimation(0, 'play' + colorIndex, false);
            }, this
        );
    },


    showBomb: function (bombOffset) {
        this.bomb.active = true;
        this.bombRegion.active = false;
        this.bomb.x = this._bombX;
        this.bomb.y = this._bombY + bombOffset;
    },

    hideBomb: function () {
        this.bomb.active = false;
        this.bombRegion.active = false;
    },

    updateBombPos: function (x, y) {
        this.bomb.x = this._bombX + x;
        this.bomb.y = this._bombY + y;
    },

    updateBombRegion: function (x, y) {
        if (x === null) {
            this.bombRegion.active = false;
            return;
        }

        this.bombRegion.x = x;
        this.bombRegion.y = y;
        this.bombRegion.active = true;
    },

    showBombSkill: function (x, y) {
        this.hideBomb();
        this.bombAn.node.active = true;
        this.bombAn.node.stopAllActions();
        this.bombAn.play();
        this.bombAn.node.x = x;
        this.bombAn.node.y = y;
        require('CommonUtil').delay(
            this.bombAn.node,
            BLAST_DELAY,
            function () {
                this.bombP.resetSystem();
            }, this
        );
    },

    // update (dt) {},
});
